home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 05 / listings / fos_test.c < prev    next >
C/C++ Source or Header  |  1988-03-04  |  13KB  |  436 lines

  1. /************************************************************************/ 
  2. /*                                                                      */ 
  3. /*                                FOS_TEST.C                                */ 
  4. /*                                                                          */ 
  5. /*        This is just a conglomeration of C code that I used to test        */ 
  6. /*       the FOSSIL interface. It is a super-kludge of misc functions        */ 
  7. /*        which do all sorts of things, some of which may actually be        */ 
  8. /*                  useful to someone (who, I don't know!)                */ 
  9. /*                                                                        */
  10. /*  BRIEF Tab setting is 4 9 13 (Don't ask why, I screwed up my system)    */
  11. /*                                                                        */
  12. /************************************************************************/ 
  13.  
  14. #include    <stdarg.h>
  15. #include    <stdio.h>
  16. #include    <time.h>
  17. #include    "fossil.h"
  18.  
  19. #define        RETRY        -1
  20. #define        IGNORE        -2
  21. #define        ABORT        -3
  22. #define        NORETRY        -4
  23. #define        TIMEOUT        -5
  24.  
  25. struct    _resultcodes    {
  26.         char *    string;
  27.         int        returncode;
  28.         int        baudrate;
  29. }    result[] = {
  30.                 "CONNECT FAST",        192,        19200,
  31.                 "CONNECT 9600",        96,            9600,
  32.                 "CONNECT 4800",        48,            4800,
  33.                 "CONNECT 2400",        24,            2400,
  34.                 "CONNECT 1200",        12,            1200,
  35.                 "CONNECT 600",        6,            600,
  36.                 "CONNECT ",            3,            300,
  37.                 "RING",                IGNORE,        0,
  38.                 "RRING",            IGNORE,        0,
  39.                 "BUSY",                RETRY,        0,
  40.                 "NO CARRIER",        RETRY,        0,
  41.                 "NO DIALTONE",        ABORT,        0,
  42.                 "VOICE",            NORETRY,    0,
  43.                 "OK",                IGNORE,        0,
  44.                 "ERROR",            RETRY,        0,
  45.  
  46.                 NULL,                ABORT,        0
  47. };
  48.  
  49.  
  50. char        txbuf[] =
  51. "This is a simple test of FOSSIL's ability to send a large block of characters \
  52. using the block write command of a draft 5 FOSSIL. Hopefully, It will work well \
  53. because that function is helpful during block oriented protocols.";
  54.  
  55. int        echo_on    = 0;
  56. int        local_on= 0;
  57. int        baud_rate_locked= 1;
  58. char        d_prefix[80] = "AT DT";
  59. char        d_suffix[80] = "\n";
  60. long        timers[10] = {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
  61.  
  62. /************************************************************************/ 
  63. /*                                                                      */ 
  64. /*        APPARANTLY, THE TIMER TICK STEALING EITHER DOES NOT WORK IN        */ 
  65. /*         MSC 5 DUE TO OPTIMIZATION, OR THE FOSSIL I AM TESTING ON        */ 
  66. /*       HAS A BUG, OR MY CODE HAS A BUG. I CAN NOT SEEM TO LOCATE IT        */ 
  67. /*           SO IF ANY OF YOU PEOPLE FIND IT, PLEASE LET ME KNOW!            */ 
  68. /*                                                                        */ 
  69. /************************************************************************/ 
  70.  
  71. #pragma    check_stack(off)
  72.  
  73. static int    counter    = 0;
  74.  
  75.  far
  76. tick_tock()
  77. {
  78.     int far * i;
  79.  
  80.     i=&counter;
  81.     (*i)++;
  82. }
  83.  
  84. #pragma    check_stack()
  85.  
  86. /************************************************************************/ 
  87. /*                                                                      */ 
  88. /*        The f_printf() function works as advertised, but be wary of        */ 
  89. /*       using \b's, etc. It may not give you exactly what you wanted        */ 
  90. /*                                                                        */ 
  91. /************************************************************************/ 
  92.  
  93. f_printf(port,string,...)
  94.  int    port;
  95.  char *    string;
  96. {
  97.     char    buffer[128];
  98.     char *    str;
  99.  
  100.     va_list    arg;
  101.     va_start(arg,string);
  102.     vsprintf(buffer,string,arg);
  103.     va_end(arg);
  104.     
  105.     str = buffer;
  106.     while (*str) {
  107.         f_tx(port,*str);
  108.         if (*str == '\n')
  109.             f_tx(port,13);
  110.         str++;
  111.     }
  112. }
  113.  
  114. /************************************************************************/ 
  115. /*                                                                      */ 
  116. /*       This function sends 30,000 bytes to the transmitter, filling        */ 
  117. /*           the buffer (note the f_txnowait call) then returning.        */ 
  118. /*            I use this function for testing the purge and flush            */ 
  119. /*                         functions of the FOSSIL.                        */ 
  120. /*                                                                        */ 
  121. /************************************************************************/ 
  122.  
  123. txtest(port)
  124.  int    port;
  125. {
  126.     int        q;
  127.     int        c='A';
  128.  
  129.     for (q=0;q<30000;q++) {
  130.         c++;
  131.         if (c > 'Z')
  132.             c = 'A';
  133.         f_txnowait(port,c);
  134.     }
  135. }
  136.  
  137. /************************************************************************/ 
  138. /*                                                                      */ 
  139. /*        This function simply prints the return from the timer tick        */ 
  140. /*         diagnostic functions in the FOSSIL. On an IBM, the values        */ 
  141. /*                         should be 8, 18, and 55.                        */ 
  142. /*                                                                        */ 
  143. /************************************************************************/ 
  144.  
  145. tticktest()
  146. {
  147.     printf("Timer tick values:\n\n");
  148.     printf(" --- Interrupt on vector 0x%02x\n",f_ttint());
  149.     printf(" --- Ticks per second = %u\n",f_ttspeed());
  150.     printf(" --- Milliseconds per tick = %u\n",f_ttmilli());
  151. }
  152.  
  153. /************************************************************************/ 
  154. /*                                                                      */ 
  155. /*        This tests the flush function. There should be a noticable        */ 
  156. /*               delay between the beep and the word FLUSHED.                */ 
  157. /*                                                                        */ 
  158. /************************************************************************/ 
  159.  
  160. flushtest(port)
  161.  int    port;
  162. {
  163.     txtest(port);
  164.     printf("FLUSHING ....\a");
  165.     f_outflush(port);
  166.     printf("FLUSHED\n");
  167. }
  168.  
  169. /************************************************************************/ 
  170. /*                                                                      */ 
  171. /*        This tests purging, there should not be a delay between the        */ 
  172. /*                         beep and the word PURGED.                        */ 
  173. /*                                                                        */ 
  174. /************************************************************************/ 
  175.  
  176. purgetest(port)
  177.  int    port;
  178. {
  179.     txtest(port);
  180.     printf("PURGING ....\a");
  181.     f_outpurge(port);
  182.     printf("PURGED\n");
  183. }
  184.  
  185. /************************************************************************/ 
  186. /*                                                                      */ 
  187. /*                   This displays the FOSSIL status word                    */ 
  188. /*                                                                        */ 
  189. /************************************************************************/ 
  190.  
  191. status(port)
  192.  int    port;
  193. {
  194.     unsigned int    stat;
  195.     
  196.     stat = f_stat(port);
  197.     f_printf(port,"Status word reads as 0x%04x\n",stat);
  198. }
  199.  
  200. /************************************************************************/ 
  201. /*                                                                      */ 
  202. /*          This function prints some stuff, then asks the user to        */ 
  203. /*                   press the space bar on the terminal.                    */ 
  204. /*                                                                        */ 
  205. /************************************************************************/ 
  206.  
  207. txrxtest(port)
  208.  int    port;
  209. {
  210.     unsigned int    space;
  211.  
  212.     f_printf(port,"Hello, world!\n");
  213.     f_printf(port,"Press the SPACE BAR here now!\n\n");
  214.     space = f_rx(port);
  215.     if (space == ' ')
  216.         f_printf(port,"\nGood!\n");
  217.     else
  218.         f_printf(port,"I received a \'%c\' character (0x%04x)\n",space,space);
  219. }
  220.  
  221. /************************************************************************/ 
  222. /*                                                                      */ 
  223. /*        This function does a gets() type function, but will timeout        */ 
  224. /*                            if timer 0 alarms.                            */ 
  225. /*                                                                        */ 
  226. /************************************************************************/ 
  227.  
  228. f_gets(port,buffer)
  229.  int    port;
  230.  char *    buffer;
  231. {
  232.     int        character;
  233.     
  234.     *buffer = 0;
  235.  
  236.     while (!expired(0)) {
  237.         character = f_peek(port);
  238.         if (character != 0xffff) {
  239.             character = f_rx(port);
  240.             character &= 0x00ff;
  241.             *buffer++ = character;
  242.             *buffer = 0;
  243.             if (echo_on) {
  244.                 f_txnowait(port,character);
  245.                 if (character == 13)
  246.                     f_txnowait(port,10);
  247.             }
  248.             if (local_on) {
  249.                 f_wransi(character);
  250.                 if (character == 13)
  251.                     f_wransi(10);
  252.             }
  253.             if (character == 13)
  254.                 return;
  255.         }
  256.     }
  257.     return;
  258. }
  259.  
  260. /************************************************************************/ 
  261. /*                                                                      */ 
  262. /*                               NON-PORTABLE!                            */ 
  263. /*                                                                          */ 
  264. /*         This function sets a timer to "alarm" (go active) after a        */ 
  265. /*                       specified number of seconds.                        */ 
  266. /*                                                                        */ 
  267. /************************************************************************/ 
  268.  
  269. set_expiration(timer,seconds)
  270.  int    timer;
  271.  int    seconds;
  272. {
  273.     long far *    tick = (long far *) 0x0000046cL;
  274.     long        increment;
  275.  
  276.     increment = ((long) seconds) * 1821L;
  277.     increment /= 100L;
  278.     increment += 9L;
  279.     timers[timer] = *tick + increment;
  280. }
  281.  
  282. /************************************************************************/ 
  283. /*                                                                      */ 
  284. /*                               NON-PORTABLE!                            */ 
  285. /*                                                                          */ 
  286. /*                This indicates whether a timer has alarmed.                */ 
  287. /*                                                                        */ 
  288. /************************************************************************/ 
  289.  
  290. expired(timer)
  291.  int    timer;
  292. {
  293.     long far *    tick = (long far *) 0x0000046cL;
  294.  
  295.     return ((timers[timer] > *tick) ? 0 : 1);
  296. }
  297.  
  298. /************************************************************************/ 
  299. /*                                                                      */ 
  300. /*         This function looks for a specific string from the FOSSIL        */ 
  301. /*         and reacts accordingly. You can use this to set baud rate        */ 
  302. /*                 based on the inbound string from a modem.                */ 
  303. /*                                                                        */ 
  304. /************************************************************************/ 
  305.  
  306. f_result(port,time)
  307.  int    port;
  308.  int    time;
  309. {
  310.     char    line[128];
  311.     int        q;
  312.  
  313.     set_expiration(0,time);
  314.     while (!expired(0)) {
  315.         f_gets(port,line);
  316.         if (expired(0))
  317.             return(TIMEOUT);
  318.         q=0;
  319.         while (result[q].string != NULL) {
  320.             if (!strncmp(line,result[q].string,strlen(result[q].string))) {
  321.                 if (result[q].returncode != IGNORE) {
  322.                     if (!baud_rate_locked && result[q].baudrate != 0)
  323.                         f_baud(port,result[q].baudrate);
  324.                     return(result[q].returncode);
  325.                 }
  326.             }
  327.             q++;
  328.         }
  329.     }
  330.     return (RETRY);
  331. }
  332.  
  333. /************************************************************************/ 
  334. /*                                                                      */ 
  335. /*          This function prints EVERYTHING that you wanted to know        */ 
  336. /*                   about the FOSSIL you have installed!                    */ 
  337. /*                                                                        */ 
  338. /************************************************************************/ 
  339.  
  340. data(port)
  341.  int    port;
  342. {
  343.     struct _fossildata    f;
  344.  
  345.     f_data(port,sizeof(f),&f);
  346.     printf("FOSSIL Data Structure\n\n");
  347.     printf("size of the structure in bytes      :%d\n",f.strsiz);
  348.     printf("FOSSIL spec driver conforms to      :%d\n",f.majver);
  349.     printf("rev level of this specific driver   :%d\n",f.minver);
  350.     printf("ASCII ID string                        :%Fs\n",f.ident);
  351.     printf("size of the input buffer (bytes)    :%d\n",f.ibufr);
  352.     printf("number of bytes left in buffer      :%d\n",f.ifree);
  353.     printf("size of the output buffer (bytes)   :%d\n",f.obufr);
  354.     printf("number of bytes left in the buffer  :%d\n",f.ofree);
  355.     printf("width of screen on this adapter     :%d\n",f.swidth);
  356.     printf("height of screen                    :%d\n",f.sheight);
  357.     printf("ACTUAL baud rate, computer to modem :%d\n",f.baud);
  358.  
  359. }
  360.  
  361. /************************************************************************/ 
  362. /*                                                                      */ 
  363. /*                 This function tests the writeblk function                */ 
  364. /*                                                                        */ 
  365. /************************************************************************/ 
  366.  
  367. blocktest(port)
  368.  int    port;
  369. {
  370.      f_printf(port,"Sending %d characters using writeblock\n\n",sizeof(txbuf));
  371.     f_writeblk(port,sizeof(txbuf),txbuf);
  372.     f_outflush(port);
  373. }
  374.  
  375.  
  376. /************************************************************************/
  377. /*                                                                        */ 
  378. /*   Some functions from an old terminal program I once wrote but will    */
  379. /*                             never claim.                                */
  380. /*                                                                        */ 
  381. /************************************************************************/ 
  382.  
  383. setdial(prefix,suffix)
  384.  char *    prefix;
  385.  char *    suffix;
  386. {
  387.     strcpy(d_prefix,prefix);
  388.     strcpy(d_suffix,suffix);
  389. }
  390.  
  391.  int
  392. dial(port, number)
  393.  int    port;
  394.  char *    number;
  395. {
  396.     int                    result;
  397.  
  398.     f_printf(port,"%s%s%s",d_prefix,number,d_suffix);
  399.     result = f_result(port,30);
  400.     if (result > 0)
  401.         return(0);
  402.     return(result*-1);
  403. }
  404.  
  405. /************************************************************************/ 
  406. /*                                                                        */ 
  407. /*                              Good ole main()                            */ 
  408. /*                                                                        */ 
  409. /************************************************************************/ 
  410.  
  411. main()
  412. {
  413.     int        port = 0;        /* COM1: */
  414.     int        baud = 2400;    /* 2400     */
  415.  
  416.     echo_on    = 1;
  417.     local_on= 1;
  418.     baud_rate_locked= 1;
  419.  
  420.     f_init(port,0,NULL);
  421.     f_baud(port,baud);
  422.     data(port);
  423.     setdial("AT DT","\n");
  424.     dial(port,"17034948331");
  425.     txtest(port);
  426.     tticktest();
  427.     flushtest(port);
  428.     purgetest(port);
  429.     status(port);
  430.     txrxtest(port);
  431.     blocktest(port);
  432.     f_outflush(port);
  433.     f_deinit(port);
  434. }
  435.  
  436.